package com.stars.common.gen;

import com.stars.common.valid.MyException;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName GenerateCodeBtlUtils
 * @Description: 根据String btl 生成代码工具
 * @Author DJ
 * @Date 2019/10/25
 * @Version V1.0
 **/
public class GenerateCodeBtlUtils {
    /**
     * 生成代码
     * @param btl
     * @param outFilePath
     * @param outFileName
     * @param paramMap
     */
    public static void generateCodeBtl(String btl, String outFilePath, String outFileName, Map<String, Object> paramMap) {
        checkBase(btl, outFilePath, outFileName, paramMap);
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            throw new MyException(e.getMessage());
        }
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        //获取模板
        Template t = gt.getTemplate(btl);
        //设置参数
        paramMap.forEach((key, value) -> {
            t.binding(key, value);
        });
        generateFile(t, outFilePath, outFileName);
    }

    /**
     * 生成指定文件路径
     *
     * @param t btl模板
     * @param outFilePath 文件路径
     * @param outFileName 文件名称
     */
    private static void generateFile(Template t, String outFilePath, String outFileName) {
        Writer out = null;
        try {
            File file = new File(outFilePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File docFile = new File(outFilePath + "/" + outFileName);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            t.renderTo(out);
        } catch (FileNotFoundException e) {
            throw new MyException("生成文件异常" + e.getMessage());
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                throw new MyException("生成文件异常" + e2.getMessage());
            }
        }
    }

    /**
     * 基础校验
     *
     * @param btl
     * @param outFile
     * @param outFileName
     * @param paramMap
     */
    private static void checkBase(String btl, String outFile, String outFileName, Map<String, Object> paramMap) {
        if (StringUtils.isBlank(btl)) {
            throw new MyException("btl 模板不能为空");
        }
        if (StringUtils.isBlank(outFile)) {
            throw new MyException("输出文件路径不能为空");
        }
        if (StringUtils.isBlank(outFileName)) {
            throw new MyException("输出文件名称不能为空");
        }
        if (null == paramMap) {
            throw new MyException("模板参数值不能为空");
        }
    }

    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","dj");
        Test1  t = new Test1();
        t.setAge(340);
        t.setName("李白");
        map.put("t",t);
        generateCodeBtl("hello,${name},${t.name}","D:/test.dd","test.java",map);
    }
}
